#!/usr/bin/env python
import re
from datetime import datetime
if __name__ == "__main__":
    worker_pattern = re.compile(r'Default Balance Policy \[(\w+)\] Max Worker Num\[(\d+)\] Min Worker Num\[(\d+)\] Role\[\w+\] worker size \[(\d+)\]')
    qps_pattern = re.compile(r'qps=(\d+\.\d+).delay=(\d+\.\d+)')
    file_object = open('/home/wangxin/work_station/master.log')
    output = open('data.dat', 'w')
    try:
        i = 0
        isBegin = False
        size = 0
        qps_all=0.0
        delay_all=0.0
        role_name = ''
        max_worker = 0
        min_worker = 0
        cur_worker = 0
        time_str = ''
        time_object = None
        for line in file_object:
            i += 1
            pos = line.find('Default Balance Policy')
            if pos > 0:
                isBegin = True
                time_str = line[0:19]
                time_object = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
                current = line[pos:]
                match = worker_pattern.match(current)
                if match:
                    role_name = match.group(1)
                    max_worker = int(match.group(2))
                    min_worker = int(match.group(3))
                    cur_worker = int(match.group(4))
                    size = cur_worker

            else:
                if size > 0:
                    pos1 = line.find("theoryqps", 40)
                    pos2 = line.find("Source", 50)
                    current = line[pos1:pos2]
                    pos3 = current.rfind("qps")
                    current = current[pos3:]
                    if pos1 > 0 and pos2 > 0:
                        match = qps_pattern.match(current)
                        if match:
                            qps = float(match.group(1))
                            qps_all += qps
                            delay = float(match.group(2))
                            delay_all += delay
                        else:
                            print current
                    size -= 1
                    if size == 0:
                        output.write("%s,%d,%d,%d\n" % (time_str, cur_worker, qps_all, delay_all/cur_worker))
                        qps_all = 0.0
                        delay_all = 0.0
        print("Total line number : %d." % i)
    finally:
        file_object.close()
        output.close()
